package Hibernate.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for Score
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see Hibernate.dao.Score
 * @author MyEclipse Persistence Tools
 */
public class ScoreDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(ScoreDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String EXPERT_ID = "expertId";
	public static final String EXAM_ID = "examId";
	public static final String FIRST_SCORE = "firstScore";
	public static final String FINAL_SCORE = "finalScore";
	public static final String SCORE_TIME = "scoreTime";

	public void save(Score transientInstance) {
		log.debug("saving Score instance");
		Transaction tr = getSession().beginTransaction(); 
		try {
			getSession().save(transientInstance);
			tr.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public void delete(Score persistentInstance) {
		log.debug("deleting Score instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Score findById(java.lang.Integer id) {
		log.debug("getting Score instance with id: " + id);
		Transaction tr = getSession().beginTransaction(); 
		try {
			Score instance = (Score) getSession()
					.get("Hibernate.dao.Score", id);
			tr.commit();
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByExample(Score instance) {
		log.debug("finding Score instance by example");
		Transaction tr = getSession().beginTransaction(); 
		try {
			List results = getSession().createCriteria("Hibernate.dao.Score")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			tr.commit();
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Score instance with property: " + propertyName
				+ ", value: " + value);
		Transaction tr = getSession().beginTransaction(); 
		try {
			String queryString = "from Score as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}
	
	public List findByUserIdExpertId( String userValue, String expertValue){
		Transaction tr = getSession().beginTransaction(); 
		try {
			String queryString = "from Score as model where model."
					+ USER_ID + "= ? and model." + EXPERT_ID + "= ? ";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, userValue);
			queryObject.setParameter(1, expertValue);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByExpertId(Object expertId) {
		return findByProperty(EXPERT_ID, expertId);
	}

	public List findByExamId(Object examId) {
		return findByProperty(EXAM_ID, examId);
	}

	public List findByFirstScore(Object firstScore) {
		return findByProperty(FIRST_SCORE, firstScore);
	}

	public List findByFinalScore(Object finalScore) {
		return findByProperty(FINAL_SCORE, finalScore);
	}

	public List findByScoreTime(Object scoreTime) {
		return findByProperty(SCORE_TIME, scoreTime);
	}

	public List findAll() {
		log.debug("finding all Score instances");
		Transaction tr = getSession().beginTransaction(); 
		try {
			String queryString = "from Score";
			Query queryObject = getSession().createQuery(queryString);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public Score merge(Score detachedInstance) {
		log.debug("merging Score instance");
		try {
			Score result = (Score) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Score instance) {
		log.debug("attaching dirty Score instance");
		Transaction tr = getSession().beginTransaction();
		try {
			getSession().saveOrUpdate(instance);
			tr.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public void attachClean(Score instance) {
		log.debug("attaching clean Score instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}